package com.myhome.controller;


import com.myhome.entity.Position;
import com.myhome.entity.RespBean;
import com.myhome.entity.Role;
import com.myhome.service.IPositionService;
import com.myhome.service.IRoleService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2021-09-04
 */
@RestController
@RequestMapping("/system/basic/role")
public class RoleController {
    @Autowired
    IRoleService roleService;

    @ApiOperation(value = "获取全部权限")
    @GetMapping("/")
    public List<Role> getAllPosition() {
        List<Role> result = roleService.list();
        return result;
    }

    @ApiOperation(value = "增加权限信息")
    @PostMapping("/putPosition")
    public RespBean putPosition(@RequestBody Role role) {
        if (!role.getName().startsWith("ROLE_")) {
            role.setName("ROLE_" + role.getName());
        }
        if (roleService.save(role)) {
            return RespBean.success("添加成功");
        }
        return RespBean.error("添加失败");
    }


    @ApiOperation(value = "删除权限信息")
    @DeleteMapping("/delete/{id}")
    public RespBean deletePosition(@PathVariable("id") Integer id) {
        if (roleService.removeById(id)) {
            return RespBean.success("删除成功");
        }
        return RespBean.error("删除失败");
    }

}
